<?php

/*
=====================================================
 FluxBB - Delux Board Converstations (DBC)
-----------------------------------------------------
 http://dbc-forum.com/
 http://fluxbb.org/
-----------------------------------------------------
 Copyright (c) 2010,2011 FluxBB - Delux Board Conversations (DBC)
-----------------------------------------------------
 Предсловие: Данный скрипт (DBC) не является работой с нуля, то 
 есть ядром скрипта всё также остаётся FuxBB,но теперь в оболочке
 DBC.
=====================================================
*/

define('DBC_ROOT', dirname(__FILE__).'/');
require DBC_ROOT.'engine/common.php';

if ($DBC_user['g_read_board'] == '0')
	message($lang_common['No view']);

$bid = isset($_GET['bid']) ? intval($_GET['bid']) : 0;

// Load the viewtopic.php language file
require DBC_ROOT.'language/'.$DBC_user['language'].'/forum.php';
require DBC_ROOT.'language/'.$DBC_user['language'].'/topic.php';
	
// Load the viewforum.php language file
if (file_exists(DBC_ROOT.'language/'.$DBC_user['language'].'/news.php'))
	require DBC_ROOT.'language/'.$DBC_user['language'].'/news.php';
else
	require DBC_ROOT.'language/English/news.php';

$message_len = 1000; // Short message for Not Admin
$DBC_user['disp_topics'] = 5; // Number of topics to display per page
// Display topics number
$last_topics = 10;
$most_replies = 10;
$most_views =  10;
$cut_str = 17; // Topics subject

// Query all forums? 
if ($bid < 1)
{
	$bid = 0;
	$sql_fid = '';
}
else
{	
	$sql_fid = ' AND f.id='.$bid;
	// Fetch some info about the forum
	if (!$DBC_user['is_guest'])
		$result = $db->query('SELECT f.forum_name, f.redirect_url, f.moderators, f.num_topics, f.sort_by, fp.post_topics, s.user_id AS is_subscribed FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_subscriptions AS s ON (f.id=s.forum_id AND s.user_id='.$DBC_user['id'].') LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1)'.$sql_fid) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
	else
		$result = $db->query('SELECT f.forum_name, f.redirect_url, f.moderators, f.num_topics, f.sort_by, fp.post_topics, 0 AS is_subscribed FROM '.$db->prefix.'forums AS f LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1)'.$sql_fid) or error('Unable to fetch forum info', __FILE__, __LINE__, $db->error());
	
	$cur_forum = $db->fetch_assoc($result);	
	if ($cur_forum['redirect_url'] != '')
	{
		header('Location: '.$cur_forum['redirect_url']);
		exit;
	}
}


// Can we or can we not post new topics?
$post_link = '';
if ($bid > 0)
{
	// Sort out who the moderators are and if we are currently a moderator (or an admin)
	$mods_array = ($cur_forum['moderators'] != '') ? unserialize($cur_forum['moderators']) : array();
	$is_admmod = ($DBC_user['g_id'] == DBC_ADMIN || ($DBC_user['g_moderator'] == '1' && array_key_exists($DBC_user['username'], $mods_array))) ? true : false;

	if (($cur_forum['post_topics'] == '' && $DBC_user['g_post_topics'] == '1') || $cur_forum['post_topics'] == '1' || $is_admmod)
		$post_link = "\t\t\t".'<p class="postlink conr"><a href="post_fid'.$bid.'">'.$lang_forum['Post topic'].'</a></p>'."\n";
	else
		$post_link = '';
}	

// Count topics	
if ($DBC_user['g_id'] == DBC_ADMIN)
{
	$result = $db->query('SELECT COUNT(t.id), t.forum_id, t.moved_to FROM '.$db->prefix.'topics as t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=3) WHERE t.moved_to IS NULL'.$sql_fid) or error('Unable to get the admin\'s topic list', __FILE__, __LINE__, $db->error());
}   
elseif ($DBC_user['is_guest'])
{
	$result = $db->query('SELECT COUNT(t.id), t.forum_id, t.moved_to FROM '.$db->prefix.'topics as t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=3) WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.moved_to IS NULL'.$sql_fid) or error('Unable to get the guest\'s topic list', __FILE__, __LINE__, $db->error());
}
else
{
	$result = $db->query('SELECT COUNT(t.id), t.forum_id, t.moved_to FROM '.$db->prefix.'topics as t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.moved_to IS NULL'.$sql_fid) or error('Unable to get the member\'s topic list', __FILE__, __LINE__, $db->error());
}

$num_topics = $db->result($result);
$num_pages = ceil($num_topics / $DBC_user['disp_topics']);//$DBC_user['disp_posts'];

$p = (!isset($_GET['p']) || $_GET['p'] <= 1 || $_GET['p'] > $num_pages) ? 1 : intval($_GET['p']);
$start_from = $DBC_user['disp_topics'] * ($p - 1);

// Generate paging links
$paging_links = '<span class="pages-label">'.$lang_common['Pages'].' </span>'.paginate($num_pages, $p, 'news_feed?bid='.$bid);

$page_title = array(DBC_htmlspecialchars($DBC_config['o_board_title']), $lang_common['Newsfeed']);
define('DBC_ALLOW_INDEX', 1);
define('DBC_ACTIVE_PAGE', 'index');
require DBC_ROOT.'engine/engine.php';
$show = '5'; // Change 5 by the number of topics you want the mod to display
require DBC_ROOT.'engine/last_topics.php';

?>

<div class="blogright">
<?php
	generate_forumlinks($DBC_user['g_id']);
	most_topics('last', $last_topics);
	most_topics('most_replies', $most_replies);
	most_topics('most_views', $most_views);
?>
</div>

<div class="blogleft2">
<table width="100%" height="38" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="28" class="bg_forum_title_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_title_center"><span class="cat_title"><?php echo $lang_common['Newsfeed'] ?></span></td>
<td align="center" width="28" class="bg_forum_title_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></table>
<?php

require DBC_ROOT.'engine/parser.php';

$post_count = 0; // Keep track of post numbers

if ($DBC_user['g_id'] == DBC_ADMIN)
{
	$result = $db->query('SELECT t.id, t.poster, t.subject, t.posted, t.first_post_id, t.last_post, t.last_post_id, t.last_poster, t.closed, t.num_replies, t.num_views, t.moved_to, t.forum_id, p.poster_id, p.topic_id, p.message, p.hide_smilies FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=3) WHERE t.first_post_id=p.id AND t.moved_to IS NULL'.$sql_fid.' ORDER BY t.posted DESC LIMIT '.$start_from.', '.$DBC_user['disp_topics']) or error('Unable to get the admin\'s topic list', __FILE__, __LINE__, $db->error());
}   
elseif ($DBC_user['is_guest'])
{
	$result = $db->query('SELECT t.id, t.poster, t.subject, t.posted, t.first_post_id, t.last_post, t.last_post_id, t.last_poster, t.closed, t.num_replies, t.num_views, t.moved_to, t.forum_id, p.poster_id, p.topic_id, p.message, p.hide_smilies FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=3) WHERE t.first_post_id=p.id AND (fp.read_forum IS NULL OR fp.read_forum=1) AND t.moved_to IS NULL'.$sql_fid.' ORDER BY t.posted DESC LIMIT '.$start_from.', '.$DBC_user['disp_topics']) or error('Unable to get the guest\'s topic list', __FILE__, __LINE__, $db->error());
}
else
{
	$result = $db->query('SELECT t.id, t.poster, t.subject, t.posted, t.first_post_id, t.last_post, t.last_post_id, t.last_poster, t.closed, t.num_replies, t.num_views, t.moved_to, t.forum_id, p.poster_id, p.topic_id, p.message, p.hide_smilies FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'posts AS p ON t.id=p.topic_id INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') WHERE t.first_post_id=p.id AND (fp.read_forum IS NULL OR fp.read_forum=1) AND t.moved_to IS NULL'.$sql_fid.' ORDER BY t.posted DESC LIMIT '.$start_from.', '.$DBC_user['disp_topics']) or error('Unable to get the member\'s topic list', __FILE__, __LINE__, $db->error());
}

if ($db->num_rows($result)) // If there are topics
{
	while ($cur_topic = $db->fetch_assoc($result)) {
		$post_count++;
		$post_actions = array();
		
		if ($DBC_config['o_censoring'] == '1')
			$cur_topic['subject'] = censor_words($cur_topic['subject']);

		// Generation post action array (quote, edit, delete etc.)
		if ($DBC_user['g_id'] == DBC_ADMIN)
		{
			$post_actions[] = '<li class="postreport"><span><a href="report_'.$cur_topic['id'].'">'.$lang_topic['Report'].'</a></span></li>';
			$post_actions[] = '<li class="postdelete"><span><a href="delete_'.$cur_topic['first_post_id'].'">'.$lang_topic['Delete'].'</a></span></li>';
			$post_actions[] = '<li class="postedit"><span><a href="edit_'.$cur_topic['first_post_id'].'">'.$lang_topic['Edit'].'</a></span></li>';
			$post_actions[] = '<li class="postquote"><span><a href="post_tid'.$cur_topic['id'].'&amp;qid='.$cur_topic['first_post_id'].'">'.$lang_topic['Quote_ajax'].'</a></span></li>';
			if ($cur_topic['closed'] == '0')
			$post_actions[] = '<li class="postreply"><span><a href="post_tid'.$cur_topic['id'].'">'.$lang_topic['Post reply'].'</a></span></li>';
		}
		else
		{
			if (!$DBC_user['is_guest'])
			$post_actions[] = '<li class="postreport"><span><a href="report_'.$cur_topic['first_post_id'].'">'.$lang_topic['Report'].'</a></span></li>';

			if ($cur_topic['closed'] == '0')
			{
				if ($cur_topic['poster_id'] == $DBC_user['id'])
				{
					if (($DBC_user['g_delete_topics'] == '1') || $DBC_user['g_delete_posts'] == '1')
					$post_actions[] = '<li class="postdelete"><span><a href="delete_'.$cur_topic['first_post_id'].'">'.$lang_topic['Delete'].'</a></span></li>';
					if ($DBC_user['g_edit_posts'] == '1')
						$post_actions[] = '<li class="postedit"><span><a href="edit_'.$cur_topic['first_post_id'].'">'.$lang_topic['Edit'].'</a></span></li>';
				}

				if (($cur_topic['post_replies'] == '' && $DBC_user['g_post_replies'] == '1') || $cur_topic['post_replies'] == '1')
				$post_actions[] = '<li class="postquote"><span><a href="post_tid'.$cur_topic['id'].'&amp;qid='.$cur_topic['first_post_id'].'">'.$lang_topic['Quote'].'</a></span></li>';
				
				if (($cur_topic['post_replies'] == '' && $DBC_user['g_post_replies'] == '1') || $cur_topic['post_replies'] == '1')
				$post_actions[] = '<li class="postreply"><span><a href="post_tid'.$cur_topic['id'].'">'.$lang_topic['Post reply'].'</a></span></li>';
			}
		}
		
		// Short message for Not Admin
		$short = false;
		if ((mb_strlen($cur_topic['message'], 'UTF-8') > $message_len) && ($DBC_user['g_id'] != DBC_ADMIN))
		{
			$message_len++;
			$tmp = mb_substr($cur_topic['message'], 0, $message_len, 'UTF-8');
			while(!parse_message($tmp, $cur_topic['hide_smilies']))
			{
				$message_len++;
				$tmp = mb_substr($cur_topic['message'], 0, $message_len, 'UTF-8');
			}
			if ($cur_topic['message'] != $tmp) $short = true;
			// Perform the main parsing of the message (BBCode, smilies, censor words etc)
			$cur_topic['message'] = parse_message($tmp, $cur_topic['hide_smilies']);
		}
		else
		{
			// Perform the main parsing of the message (BBCode, smilies, censor words etc)
			$cur_topic['message'] = parse_message($cur_topic['message'], $cur_topic['hide_smilies']);
		}
?>


<div id="p<?php echo $cur_topic['first_post_id'] ?>" class="blockpost<?php echo ($post_count % 2 == 0) ? ' roweven' : ' rowodd' ?>">
	<h2><span><span class="conr">(<a href="topics_<?php echo $cur_topic['first_post_id'].'#p'.$cur_topic['first_post_id'] ?>"><?php echo format_time($cur_topic['posted']) ?></a>)&nbsp;&nbsp;#<?php echo ($start_from + $post_count) ?></span> <a href="topics_<?php echo $cur_topic['first_post_id'].'#p'.$cur_topic['first_post_id'] ?>"><?php echo DBC_htmlspecialchars($cur_topic['subject']) ?></a></span></h2>
	<div class="box">
		<div class="inbox">
			<div class="blogpostmsg">

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left" width="65" valign="top"><span class="news_avatar"><a href="user_<?php echo DBC_htmlspecialchars($cur_topic['poster_id']) ?>" title="<?php echo $lang_common['Profile'] ?> <?php echo $cur_topic['poster'] ?>"><?php echo generate_avatar_markup($cur_topic['poster_id']) ?></a>
</span></td>
<td align="left" valign="top">
				<?php echo $cur_topic['message']."\n" ?>
				<?php
					if($short) echo "(<a href='topics_".$cur_topic['first_post_id']."#p".$cur_topic['first_post_id']."'>".$lang_news['Read full']."</a>)\n"
				?>
</td>
</tr>
</table>

				<div class="newslinefoot">

					<?php if (count($post_actions)) echo "\t\t\t\t".'<div class="postfootright">'."\n\t\t\t\t\t".'<ul>'."\n\t\t\t\t\t\t".implode("\n\t\t\t\t\t\t", $post_actions)."\n\t\t\t\t\t".'</ul>'."\n\t\t\t\t".'</div>'."\n" ?>
				</div>
			</div>
		</div>
	</div>
</div>
	<?php
	}
}
else
{
?>
<div id="p0" class="blockpost rowodd">
	<h2><span><?php echo $lang_common['Topic']; ?></span></h2>
	<div class="box">
		<div class="inbox">
			<div class="blogpostmsg">
				<?php echo $lang_common['Never'] ?>
			</div>
		</div>
	</div>
</div>

<?php
}
?>


</div>

<div class="viewblogfoot"></div>

<table width="100%" height="23" border="0" cellpadding="0" cellspacing="0"><thead><tr>
<td align="left" width="30" class="bg_forum_bott_left"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="left" class="bg_forum_bott_center"><img src="engine/skins/images/spacer.gif" border="0"></td>
<td align="center" width="30" class="bg_forum_bott_right"><img src="engine/skins/images/spacer.gif" border="0"></td>
</tr></thead></table>



<div class="linkst">
	<div class="inbox crumbsplus">
		<div class="pagepost">

			<p class="pagelink conl"><span class="contacts"><?php echo $paging_links ?></span></p>
<?php echo $post_link ?>
		</div>
		<div class="clearer"></div>
	</div>
</div>


<?php

$footer_style = 'newsfeed';
$DBC_config['o_quickjump'] = '0';
	$footer_style = 'search';
require DBC_ROOT.'engine/footer.php'; 

//
// Generate forum links PHP scripts
//
function generate_forumlinks($group_id = false)
{
	global $db, $lang_news, $DBC_user;

	$groups = array();

	// If a group_id was supplied, we generate the forum links cache for that group only
	if ($group_id !== false)
	{
		// Is this group even allowed to read forums?
		$result = $db->query('SELECT g_read_board FROM '.$db->prefix.'groups WHERE g_id='.$group_id) or error('Unable to fetch user group read permission', __FILE__, __LINE__, $db->error());
		$read_board = $db->result($result);

		$groups[$group_id] = $read_board;
	}
	else
	{
		// A group_id was not supplied, so we generate the forum links cache for all groups
		$result = $db->query('SELECT g_id, g_read_board FROM '.$db->prefix.'groups') or error('Unable to fetch user group list', __FILE__, __LINE__, $db->error());
		$num_groups = $db->num_rows($result);

		while ($row = $db->fetch_row($result))
			$groups[$row[0]] = $row[1];
	}

	// Loop through the groups in $groups and output the cache for each of them
	foreach ($groups as $group_id => $read_board)
	{
		if ($read_board == '1')
		{
			$result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name, f.redirect_url FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$group_id.') WHERE fp.read_forum IS NULL OR fp.read_forum=1 ORDER BY c.disp_position, c.id, f.disp_position') or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());

			if ($db->num_rows($result))
			{
?>			

<?php				
			}
		}
	}
}

function most_topics($query, $topics_num)
{
	global $db, $lang_news, $DBC_user, $cut_str;
	
	switch ($query)
	{
		case 'last':
			$sort_by = 't.last_post DESC';
			$title = $lang_news['Last topics'];
			break;
		case 'poster':
			$sort_by = 't.poster DESC';
			$title = $lang_news['Last topics'];
			break;
		case 'most_replies':
			$sort_by = 't.num_replies DESC';
			$where_sql = ' AND t.num_replies > 0';
			$title = $lang_news['Most replies'];
			break;
		case 'most_views':
			$sort_by = 't.num_views DESC';
			$where_sql = ' AND t.num_views > 0';
			$title = $lang_news['Most views'];
			break;
		default:
			$sort_by = 't.last_post DESC';
			$title = $lang_news['Last topics'];
			break;
	}

	if ($DBC_user['g_id'] == DBC_ADMIN)
	{
		$result = $db->query('SELECT t.id, t.poster, t.subject, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.num_views, t.moved_to, t.forum_id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=3) WHERE t.moved_to IS NULL'.$where_sql.' ORDER BY '.$sort_by.' LIMIT '.$topics_num) or error('Unable to get the admin\'s topic list', __FILE__, __LINE__, $db->error());
	}   
	elseif ($DBC_user['is_guest'])
	{
		$result = $db->query('SELECT t.id, t.poster, t.subject, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.num_views, t.moved_to, t.forum_id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id=3) WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.moved_to IS NULL'.$where_sql.' ORDER BY '.$sort_by.' LIMIT '.$topics_num) or error('Unable to get the guest\'s topic list', __FILE__, __LINE__, $db->error());
	}
	else
	{
		$result = $db->query('SELECT t.id, t.poster, t.subject, t.posted, t.last_post, t.last_post_id, t.last_poster, t.num_replies, t.num_views, t.moved_to, t.forum_id FROM '.$db->prefix.'topics AS t INNER JOIN '.$db->prefix.'forums AS f ON f.id=t.forum_id LEFT JOIN '.$db->prefix.'forum_perms AS fp ON (fp.forum_id=f.id AND fp.group_id='.$DBC_user['g_id'].') WHERE (fp.read_forum IS NULL OR fp.read_forum=1) AND t.moved_to IS NULL'.$where_sql.' ORDER BY '.$sort_by.' LIMIT '.$topics_num) or error('Unable to get the member\'s topic list', __FILE__, __LINE__, $db->error());
	}
?>	
				<table cellspacing="0">
				<tbody>
				<tr><td>
<?php
if ($db->num_rows($result))
{
	while ($cur_topic = $db->fetch_assoc($result)) {
		if ($DBC_config['o_censoring'] == '1')
			$cur_topic['subject'] = censor_words($cur_topic['subject']);
			
			if (mb_strlen($cur_topic['subject'])>$cut_str && strlen($cur_topic['subject'])>$cut_str)
			{
				$cur_topic['subject'] = mb_substr($cur_topic['subject'], 0,$cut_str).'...';
			}
			
			$subject = '<a href="topics_'.$cur_topic['last_post_id'].'#p'.$cur_topic['last_post_id'].'" title="'.$lang_common['by'].' '.DBC_htmlspecialchars($cur_topic['last_poster']).'&nbsp;'.format_time($cur_topic['last_post']).'">'.DBC_htmlspecialchars($cur_topic['subject']).'</a>';
?>
<?php
	}
}
else
{
?>

<?php
}
?>
				</td></tr>
				</tbody></table>
<?php
}

$footer_style = 'index';
require DBC_ROOT.'engine/footer.php';